/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PlayerInfoPanel.java
 *
 * Created on Jul 27, 2011, 9:26:01 AM
 */
package deatax.display.gui;

import deatax.Player;
import deatax.ResourceAmount;
import deatax.network.Client;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JWindow;

/**
 *
 * @author pson
 */
public class PlayerInfoPanel extends javax.swing.JPanel {

    Player p;
    JWindow popup;
//    private ArrayList<DicePanel> dice;

    /** Creates new form PlayerInfoPanel */
    public PlayerInfoPanel() {
        initComponents();
        p = null;
        setOpaque(false);
        setBackground(new Color(0, 0, 0, 0));
        resource.setPadding(40);

        popup = new JWindow(Client.getData().getFrame());
        popup.getContentPane().setLayout(new BorderLayout());
        popup.getContentPane().add(pnlPopup, BorderLayout.CENTER);
        lblScore.addMouseListener(new ScoreMouseListener());
//        diceArea.setOpaque(false);
//        diceArea.setBackground(new Color(0,0,0,0));
//        dice = new ArrayList<DicePanel>();
//        setDice(2);
    }

    public void setPlayer(Player p) {
        this.p = p;
        refresh();
    }

    public void refresh() {
        if (p != null) {
            lblName.setForeground(p.getColor());
            lblName.setText(p.getName());
            resource.clearResources();
            popPanel.clearResources();
            for (ResourceAmount ra : p.getResources()) {
                resource.addResource(ra);
            }
            popPanel.addPopulation(p.getTotalPopulation());
            lblScore.setText("Total Score: " + p.getTotalScore());
            lblPop.setText(String.valueOf(p.getPopulationScore()));
            lblProp.setText(String.valueOf(p.getImproveScore()));
            lblLand.setText(String.valueOf(p.getTileScore()));
            lblRes.setText(String.valueOf(p.getResourceScore()));
            lblUnit.setText(String.valueOf(p.getUnitScore()));
            resource.calculate();
            popPanel.calculate();
        }

    }

//    public void setDice(int n) {
//        dice.clear();
//        diceArea.removeAll();
//        if (n > 0) {
//            dice = new ArrayList<DicePanel>();
//            for (int i = 0; i < n; ++i) {
//                DicePanel dp = new DicePanel();
//                dice.add(dp);
//                diceArea.add(dp);
//            }
//        }
//        diceArea.validate();
//    }
//    public void updateDice(Dice d) {
//        int i = 0;
//        for (DicePanel dp : dice) {
//            //System.out.println("ddd");
//            dp.setRoll(d.getPool().get(i).current());
//            i++;
//        }
//        //System.out.println("What");
//        diceArea.repaint();
//    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlPopup = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        lblPop = new javax.swing.JLabel();
        lblRes = new javax.swing.JLabel();
        lblLand = new javax.swing.JLabel();
        lblProp = new javax.swing.JLabel();
        lblUnit = new javax.swing.JLabel();
        lblName = new javax.swing.JLabel();
        resource = new deatax.display.gui.ResourcesPanel();
        popPanel = new deatax.display.gui.ResourcesPanel();
        lblScore = new javax.swing.JLabel();

        jLabel1.setText("Population Score");

        jLabel2.setText("Resource Score");

        jLabel3.setText("Land Score");

        jLabel4.setText("Property Score");

        jLabel5.setText("Unit Score");

        lblPop.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblPop.setText(" ");

        lblRes.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblRes.setText(" ");

        lblLand.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblLand.setText(" ");

        lblProp.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblProp.setText(" ");

        lblUnit.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblUnit.setText(" ");

        javax.swing.GroupLayout pnlPopupLayout = new javax.swing.GroupLayout(pnlPopup);
        pnlPopup.setLayout(pnlPopupLayout);
        pnlPopupLayout.setHorizontalGroup(
            pnlPopupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPopupLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlPopupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlPopupLayout.createSequentialGroup()
                        .addGroup(pnlPopupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlPopupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblRes, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)
                            .addComponent(lblPop, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)))
                    .addGroup(pnlPopupLayout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblLand, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE))
                    .addGroup(pnlPopupLayout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblProp, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE))
                    .addGroup(pnlPopupLayout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblUnit, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pnlPopupLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel2, jLabel3, jLabel4, jLabel5});

        pnlPopupLayout.setVerticalGroup(
            pnlPopupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlPopupLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlPopupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblPop))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlPopupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(lblRes))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlPopupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(lblLand))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlPopupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(lblProp))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlPopupLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(lblUnit))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        lblName.setFont(lblName.getFont().deriveFont(lblName.getFont().getStyle() | java.awt.Font.BOLD, lblName.getFont().getSize()+7));
        lblName.setText(" ");

        javax.swing.GroupLayout resourceLayout = new javax.swing.GroupLayout(resource);
        resource.setLayout(resourceLayout);
        resourceLayout.setHorizontalGroup(
            resourceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 268, Short.MAX_VALUE)
        );
        resourceLayout.setVerticalGroup(
            resourceLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 156, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout popPanelLayout = new javax.swing.GroupLayout(popPanel);
        popPanel.setLayout(popPanelLayout);
        popPanelLayout.setHorizontalGroup(
            popPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 145, Short.MAX_VALUE)
        );
        popPanelLayout.setVerticalGroup(
            popPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 22, Short.MAX_VALUE)
        );

        lblScore.setFont(lblScore.getFont().deriveFont(lblScore.getFont().getStyle() | java.awt.Font.BOLD, lblScore.getFont().getSize()+5));
        lblScore.setText("Total Score: 0");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(resource, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblName, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(popPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE))
                    .addComponent(lblScore, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(popPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
                    .addComponent(lblName, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(resource, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblScore)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel lblLand;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblPop;
    private javax.swing.JLabel lblProp;
    private javax.swing.JLabel lblRes;
    private javax.swing.JLabel lblScore;
    private javax.swing.JLabel lblUnit;
    private javax.swing.JPanel pnlPopup;
    private deatax.display.gui.ResourcesPanel popPanel;
    private deatax.display.gui.ResourcesPanel resource;
    // End of variables declaration//GEN-END:variables

    private class ScoreMouseListener extends MouseAdapter {

        @Override
        public void mouseEntered(MouseEvent e) {

                popup.pack();
                Point p = lblScore.getLocationOnScreen();
                popup.setLocation(p.x + 3, p.y - popup.getHeight() - 5);
                popup.setVisible(true);
            
        }

        @Override
        public void mouseExited(MouseEvent e) {
            popup.setVisible(false);
        }
    }
}
